import Vue from 'vue';
import { bucket_name } from '@/config';
const prefix = '/userProject/';

/**
 * 获取用户全部项目
 * request: cookie //为分页做准备
 */
export const userProjectGetAll = () => {
  return Vue.$http({
    url: prefix + 'getAll',
    method: 'get'
  });
};


/**
 * 新建项目
 * request: {
 *  "projectName":"", //文件名
 *  "filePathJson":"", //子目录结构
 * }
 */
export const userProjectSave = (data) => {
  return Vue.$http({
    url: prefix + 'save',
    method: 'post',
    data
  });
};

/**
 * 更新项目
 * request: {
 *  "projectId":"", //文件ID
 *  "projectName":"", //文件名
 *  "filePathJson":"", //子目录结构
 * }
 */
export const userProjectUpdate = (data) => {
  return Vue.$http({
    url: prefix + 'update',
    method: 'post',
    data
  });
};

/**
 * 删除项目
 * request: projectId:"" //项目ID
 */
export const userProjectDelete = (params) => {
  return Vue.$http({
    url: prefix + 'delete',
    method: 'get',
    params
  });
};

/**
 * 创建函数调用方法
 * request: {
 *  "codeBucketName":"", //阿里云Bucket名称
 *  "projectName":"", //函数名（项目ID）
 *  "ossObjName":"", //文件在OSS的路径
 *  "codeHandler":"index.handler", //函数入口
 *  "runTime":"python3" , //默认先都填这个
 *  "serviceName":"pangutest", //默认填这个
 *  "timeOut": 5 //超时时间
 *  }
 */
export const userProjectCreateProject = (data) => {
  return Vue.$http({
    url: prefix + 'createProject',
    method: 'post',
    data: {
      oper: data.oper,
      codeBucketName: bucket_name,
      projectName: data.id,
      ossObjName: data.ossObjName,
      codeHandler: data.handler,
      runTime: 'python3',
      serviceName: data.serviceName,
      timeOut: 50,
      projectId: data.projectId,
      usePublicLayer: data.usePublicLayers,
      customRequirements: data.customRequirements
    }
  });
};

/**
 * 函数计算，获取结果
 * request id //项目ID
 */
export const userProjectRun = (data) => {
  return Vue.$http({
    url: prefix + 'run',
    method: 'post',
    data: {
      projectName: data.id,
      serviceName: data.serviceName
    }
  });
};

/**
 * 依赖安装
 * request id // 函数 id
 */

export const userProjectRequirements = (req_id, reqs) => {
  return Vue.$http({
    url: prefix + 'requirements',
    method: 'post',
    data: {
      reqId: req_id,
      requirement: reqs
    }
  });
};

/**
 * 搜索依赖
 * name // 模糊搜索的名字
 */
// TODO 这写的不好啊 应该传当前page+pageSize 而不是limit+offset
export const searchRequirements = ({ name, limit, offset }) => {
  return Vue.$http({
    url: prefix + 'requirements/search',
    method: 'post',
    data: {
      name: name,
      limit,
      offset
    }
  });
};

/**
 * 部署
 */
export const userProjectDeploy = (functions) => {
  return Vue.$http({
    url: prefix + 'deploy',
    method: 'post',
	data:{
	 functionName:functions.functionName,
     serviceName:functions.libraryName
	}
  });
};



/**
 * 下载阿里云文件
 */
export const downloadOss = (url) => {
  return Vue.$http.get(
    url,
    {
      responseType: 'blob'
    }

  );
};

/**
 * 调用Serverless-Plugin
 * request
 *    serviceName  调用的服务名
 *    functionName 调用的函数名
 *    payload      请求的参数
 */
export const runFc = (serviceName, functionName, payload) => {
  return Vue.$http({
    url: prefix + 'runFc',
    method: 'post',
    data: {
      serviceName: serviceName,
      functionName: functionName,
      data: payload
    }
  });
};

/**
 * 调用Wiki部署接口
 */
/*export const updateWikiLib = () => {
  return Vue.$http({
    url: prefix + 'updateWiki',
    method: 'get'
  });
};
*/


/*
 * 给工程添加层
 * projectName + requirements
 */
export const createLayerForProject = (projectName, requirements) => {
  return Vue.$http({
    url: prefix + 'layer',
    method: 'post',
    data: {
      'projectName': projectName,
      'requirements': requirements
    }
  });
};

/*
 * 获取工程中层的依赖
 * projectName
 */
export const getLayerDependencyForProject = (projectName) => {
  return Vue.$http({
    url: prefix + 'layer/dependency',
    method: 'get',
    params: {
      projectName: projectName
    }
  });
};
